package com.qfedu.service.impl;

import com.qfedu.dao.AccountDao;
import com.qfedu.dao.CardsDao;
import com.qfedu.dao.impl.AccountDaoImpl;
import com.qfedu.dao.impl.CardsDaoImpl;
import com.qfedu.pojo.Account;
import com.qfedu.pojo.Cards;
import com.qfedu.service.RechargeService;
import com.qfedu.utils.JdbcUtils;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.SQLException;

public class RechargeServiceImpl implements RechargeService {
    AccountDao accountDao=new AccountDaoImpl();
    CardsDao cardsDao=new CardsDaoImpl();
    @Override
    public Cards findCardsByCardNumber(String cardnumber) {
        return cardsDao.findCardsByCardNumber(cardnumber);
    }

    @Override
    public Account findAccountByIdCode(String cardnumber) {
        return accountDao.findAccountByIdCode(cardnumber);
    }


    @Override
    public int transfer(Cards source, Account target, BigDecimal money1,BigDecimal money2) {
        Connection connection = JdbcUtils.getConnection();
        int i;
        try {
            JdbcUtils.begin(connection);

            cardsDao.outMoney(source,money1,connection);
            accountDao.inMoney(target,money2,connection);
            JdbcUtils.commit(connection);
            i=1;
        } catch (Exception e) {
            JdbcUtils.rollback(connection);
            i=0;

        }finally {
            try {
                connection.close();

            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return i;
    }
}
